//
//  CollectViewController.m
//  B-72K
//
//  Created by sos on 16/3/21.
//  Copyright © 2016年 sos. All rights reserved.
//

#import "CollectViewController.h"
#import "DataBaseManager.h"
#import "BrowesModel.h"
#import "NewsDetialViewController.h"
@interface CollectViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong) UITableView *collectTable;
@property (nonatomic, strong) NSMutableArray *collectData;
@end

@implementation CollectViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBarHidden = NO;
    self.navigationItem.title = @"我的收藏";
    // Do any additional setup after loading the view.
    self.collectTable = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
    self.collectTable.delegate = self;
    self.collectTable.dataSource = self;
    
    [self.collectTable registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    
    [self.view addSubview:self.collectTable];
    [self dataHandle];
}


-(void)dataHandle
{
    
    // 数据
    NSMutableArray *arr = [[DataBaseManager shareInstance] selectCollect];
    NSArray *dataArr = [[arr reverseObjectEnumerator] allObjects];
    self.collectData = [NSMutableArray arrayWithArray:dataArr];
    
    NSLog(@"%@",arr);

    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return self.collectData.count;
    
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    BrowesModel *m = _collectData[indexPath.row];
    cell.textLabel.text = m.title;
    
    
    return cell;
    
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NewsDetialViewController *ndVC = [[NewsDetialViewController alloc] init];
    
    BrowesModel *m = _collectData[indexPath.row];
    
    ndVC.str = m.feedId;
    
    [self.navigationController pushViewController:ndVC animated:YES];
    
}

#pragma mark - 删除
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    BrowesModel *m = _collectData[indexPath.row];
    
    [[DataBaseManager shareInstance] deleteCollectId:m];
    
    [_collectData removeObjectAtIndex:indexPath.row];
    
    [_collectTable deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:(UITableViewRowAnimationLeft)];
    if (_collectData.count == 0) {
        UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"通知" message:@"你没有任何收藏记录" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *alertAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
        [alertC addAction:alertAction];
        [self presentViewController:alertC animated:YES completion:nil];
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
